// pages/login/login.js
var util = require('../../utils/util.js')
Page({
    // 页面的初始数据
    data: {
        loading: false,
        userName: '',
        Password: '',
    },

    //输入框事件
    userName: function(e) {
        this.setData({
            userN: e.detail.value,
        })
    },
    Password: function(e) {
        this.setData({
            passW: e.detail.value,
        })
    },

    //按钮事件
    formSubmit: function() {
        if (this.data.userN.lengths == 0 || this.data.passW.lengths == 0) {
            wx.showToast({
                title: '用户名和密码不能为空',
                icon: 'loading',
                duration: 2000
            })
        } else {
            var arr = {
                account: this.data.userN,
                password: this.data.passW
            }
            util.myrequest(arr, '/staff/login', 'POST', (res) => {
                wx.setStorageSync('account', res.account);
                wx.setStorageSync('token', res.token);
                if (res.message == '登录成功'){
                  wx.showToast({
                    title: '登陆成功',
                    duration: 2000
                  })
                  setTimeout(function () {
                    wx.switchTab({
                      url: '../index/index'
                    })
                  }, 1000)
                }else{
                  wx.showToast({
                    title: '账号或密码错误',
                    duration: 2000
                  })
                }
                
            })
        }
    },
})